/* $RCSFile: ADNDynOpportunity.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/CRMWrapper3/DynEntities/ADNDynOpportunity.cs $
 * $log$
 * Revision 19 2010/10/02 14:01:49  christian.surieux
 *   Corrected a bug in allmost everyADNDyn file for PrimaryFieldVualue
 * Revision 6 2010/08/06 19:56:33  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using CrmSdk;

namespace AddonNice.CRMWrapper
{
	public class ADNDynOpportunity : DynEntity
	{
        protected class Ftor: DFactor
        {
            public Ftor() : base(CrmStr.opportunity)
            {}

            public override Type DynType
            { get { return typeof(ADNDynOpportunity); } }

            public override string DynTypename
            {   get{ return CrmStr.opportunity;}}

            public override DynEntity Build()
            {   return new ADNDynOpportunity();}

            public override DynEntity Build(CRMWrapperBase w, InternalDynamicEntity dEntity, PropertyDescriptorCollection wproperties)
            {   return new ADNDynOpportunity(w, dEntity, wproperties, null);}

            public override DynEntity Build(CRMWrapperBase w, string PrimaryFieldValue)
            {   return new ADNDynOpportunity(w,PrimaryFieldValue);}

            public override DynEntity Build(CRMWrapperBase w,Guid PrimaryKeyValue)
            {   return new ADNDynOpportunity(w,PrimaryKeyValue);}
        }

        static ADNDynOpportunity(){new Ftor();} 

        public override bool HasOwningBusinessUnitProperty
        {
            get{   return true;}
        }

        /// <summary>
		/// Reserved for template methods: do not use directly
		/// </summary>
        public ADNDynOpportunity(): base()
		{}

        public ADNDynOpportunity(CRMWrapperBase w, string PrimaryFiedlValue)
			: base(w,CrmStr.opportunity)
		{
			name = PrimaryFiedlValue;
		}

		public ADNDynOpportunity(CRMWrapperBase w, Guid CrmId)
			: base(w,CrmStr.opportunity, CrmId)
		{}

		public ADNDynOpportunity(CRMWrapperBase w, InternalDynamicEntity e)
			: base(w,CrmStr.opportunity,e,null,null)
		{}

		public ADNDynOpportunity(CRMWrapperBase w, InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s)
			: base(w,CrmStr.opportunity,e,prop,s)
		{}

        public ADNDynOpportunity(ResetWrapperHandler fn, string PrimaryFiedlValue)
			: base(fn,CrmStr.opportunity)
		{
			name = PrimaryFiedlValue;
		}

		public ADNDynOpportunity(ResetWrapperHandler fn, Guid CrmId)
			: base(fn,CrmStr.opportunity, CrmId)
		{}

		public ADNDynOpportunity(ResetWrapperHandler fn, InternalDynamicEntity e)
			: base(fn,CrmStr.opportunity,e,null,null)
		{}

		public ADNDynOpportunity(ResetWrapperHandler fn, InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s)
			: base(fn,CrmStr.opportunity,e,prop,s)
		{}
    
        /// <summary>
		/// Crm Key
		/// </summary>
        [CrmDynAttribute(CrmStr.opportunity)]
        public Guid opportunityid
		{
			get
			{
				return KeyPropertyValue(CrmStr.opportunityid);
			}
			set
			{
				AddKeyProperty(CrmStr.opportunityid, value);
			}
		}

        protected override void SetCrmIdProperty(Guid crmId)
        {
           if ( !dicProperty.ContainsKey(CrmStr.opportunityid) || KeyPropertyValue(CrmStr.opportunityid) != crmId )
                opportunityid =   crmId;
        }

        public override string PrimaryFieldName
        {
            get
            {
               return CrmStr.name;
            }
           set
           {}
        }

        /// <summary>
        /// Value contained in the PrimaryField for the DynEntity or empty if no PrimaryField
        /// </summary>
        public override string PrimaryFieldStringValue
        {
            get
            {
                return name;
            }
            set
            {
                name =   value;
            }
        }

        public override Guid PrimaryKeyGuidValue
        {
           get
           {
                return opportunityid;
           }
           set
           {
               opportunityid    =   value;
           }
        }

        /// <summary>
        /// Gets or sets the estimated date on which the opportunity is expected to close. 
        /// </summary>
        [CrmDynAttribute(CrmStr.opportunity)]
        public string estimatedclosedate
        {
            get
            {
                if ( ContainsKey(CrmStr.estimatedclosedate) )
                    return DateTimePropertyValue(CrmStr.estimatedclosedate);
                return string.Empty;
            }
            set
            {
                AddDateTimeProperty(CrmStr.estimatedclosedate,value);
            }
        }
        
        
        /// <summary>
        /// Gets or sets the estimated value of the opportunity. 
        /// </summary>
        [CrmDynAttribute(CrmStr.opportunity)]
        public decimal estimatedvalue
        {
            get
            {
                return MoneyPropertyValue(CrmStr.estimatedvalue);
            }
            set
            {
                AddMoneyProperty(CrmStr.estimatedvalue,value);
            }
        }

        /// <summary>
        /// Gets the base-currency equivalent of the actual revenue on the opportunity.
        /// </summary>
        [CrmDynAttribute(CrmStr.opportunity)]
        public decimal estimatedvalue_base
        {
            get
            {
                return MoneyPropertyValue(CrmStr.estimatedvalue_base);
            }
        }
    
        /// <summary>
        /// Gets or sets the date when the quote expires.   
        /// </summary>
        [CrmDynAttribute(CrmStr.opportunity)]
        public string actualclosedate
        {
            get
            {
                if ( ContainsKey(CrmStr.actualclosedate) )
                    return DateTimePropertyValue(CrmStr.actualclosedate);
                return string.Empty;
            }
            set
            {
                AddDateTimeProperty(CrmStr.actualclosedate,value);
            }
        }
        
        /// <summary>
        /// Gets or sets the actual revenue for the opportunity. 
        /// </summary>
        [CrmDynAttribute(CrmStr.opportunity)]
        public decimal actualvalue
        {
            get
            {
                if ( ContainsKey(CrmStr.actualvalue) )
                    return MoneyPropertyValue(CrmStr.actualvalue);
                return 0m;
            }
            set
            {
                AddMoneyProperty(CrmStr.actualvalue,value);
            }
        }

        /// <summary>
        /// Gets the base-currency equivalent of the actual revenue on the opportunity.
        /// </summary>
        [CrmDynAttribute(CrmStr.opportunity)]
        public decimal actualvalue_base
        {
            get
            {
                return MoneyPropertyValue(CrmStr.actualvalue_base);
            }
        }
        
        /// <summary>
        /// Gets the exchange rate for the currency associated with the product with respect to the base currency.
        /// </summary>
        [CrmDynAttribute(CrmStr.opportunity)]
        public decimal exchangerate
        {
            get
            {
                return DecimalPropertyValue(CrmStr.exchangerate);
            }
            set
            {
                AddDecimalProperty(CrmStr.exchangerate,value);
            }
        }

        /// <summary>
        /// Gets and sets the ID of the currency associated with the product.         
        /// </summary>
        [CrmDynAttribute(CrmStr.opportunity)]
        public Guid transactioncurrencyid
        {
            get
            {
                return LookupPropertyValue(CrmStr.transactioncurrencyid);
            }
            set
            {
                AddLookupProperty(CrmStr.transactioncurrencyid,CrmStr.transactioncurrency,value);
            }
        }

        public string transactioncurrencyName
        {
            get
            {
                return LookupPropertyName(CrmStr.transactioncurrencyid);
            }
        }

        /// <summary>
        /// Contains the description of the opportunity. 
        /// </summary>
        [CrmDynAttribute(CrmStr.opportunity)]
        public string description 
        {
            get
            {
                return StringPropertyValue(CrmStr.description);
            }
            set
            {
                AddStringProperty(CrmStr.description,value);
            }
        }

        /// <summary>
        /// Gets or sets the opportunity rating code. 
        /// </summary>
        [CrmDynAttribute(CrmStr.opportunity)]
        public int opportunityratingcode 
        {
            get
            {
                return PicklistPropertyIntValue(CrmStr.opportunityratingcode);
            }
            set
            {
                AddPicklistProperty(CrmStr.opportunityratingcode,value);
            }
        }
         
        public string opportunityratingcodeLabel
        {
            get
            {
                if ( ContainsKey(CrmStr.opportunityratingcode) )
                    return PicklistPropertyLabelValue(CrmStr.opportunityratingcode);
                return string.Empty;
            }
        }

        [CrmDynAttribute(CrmStr.opportunity)]
        public int pricingerrorcode
        {
            get
            {
                return PicklistPropertyIntValue(CrmStr.pricingerrorcode);
            }
            set
            {
                AddPicklistProperty(CrmStr.pricingerrorcode,value);
            }
        }
         
        public string pricingerrorcodeLabel
        {
            get
            {
                if ( ContainsKey(CrmStr.pricingerrorcode) )
                    return PicklistPropertyLabelValue(CrmStr.pricingerrorcode);
                return string.Empty;
            }
        }

        /// <summary>
        /// Specifies the ID of the price list associated with the product. 
        /// </summary>
        [CrmDynAttribute(CrmStr.opportunity)]
        public Guid pricelevelid
        {
            get 
            {
                if ( ContainsKey(CrmStr.pricelevelid) )
                    return LookupPropertyValue(CrmStr.pricelevelid);
                return Guid.Empty;
            }
            set
            {
                AddLookupProperty(CrmStr.pricelevelid,CrmStr.pricelevel,value);
            }
        }
 
        public string pricelevelName
        {
            get 
            {
                if ( ContainsKey(CrmStr.pricelevelid) )
                    return LookupPropertyName(CrmStr.pricelevelid);
                return string.Empty;
            }
        }

        #region customer

        string _DefaultCustomerName   =   CrmStr.account;
		public string DefaultCustomerName
		{
			get
			{
				return _DefaultCustomerName;
			}
			set
			{
                switch (value)
                {
                    case CrmStr.account:
                    case CrmStr.contact:
                        _DefaultCustomerName  =   value;
                        break;
                }
			}
		}

        /// <summary>
        /// Beware : set defaults to account, to change it set DefaultCustomerName property before calling set
        /// or use SetContactCustomerid for adding contacts
        /// </summary>
        [CrmDynAttribute(CrmStr.opportunity)]
        public Guid customerid 
        {
            get 
            {
                return CustomerPropertyValue(CrmStr.customerid);
            }
            set 
            {
                AddCustomerProperty(CrmStr.customerid,DefaultCustomerName,value);
            }
        }

        /// <summary>
        /// The entity : account or contact
        /// </summary>
        public string customerType
        {
            get 
            {
                return CustomerPropertyType(CrmStr.customerid);
            }
        }

         /// <summary>
        /// To adding contact customerids
        /// </summary>
        public void SetContactCustomerid(Guid contactid)
        {
            AddCustomerProperty(CrmStr.customerid,CrmStr.contact,contactid);
        }

        #endregion customer

        /// <summary>
        /// Gets or sets the ID of the source campaign associated with the order. 
        /// </summary>
        [CrmDynAttribute(CrmStr.opportunity)]
        public Guid campaignid
        {
            get 
            {
                if ( ContainsKey(CrmStr.campaignid) )
                    return LookupPropertyValue(CrmStr.campaignid);
                return Guid.Empty;
            }
            set
            {
                AddLookupProperty(CrmStr.campaignid,CrmStr.campaign,value);
            }
        }

        public string campaignName
        {
            get
            {
                if (ContainsKey(CrmStr.campaignid))
                    return LookupPropertyName(CrmStr.campaignid);
                return string.Empty;
            }
        }

        /// <summary>
        /// Gets or sets the probability of closing the opportunity. 
        /// </summary>
        [CrmDynAttribute(CrmStr.opportunity)]
        public int closeprobability 
        {
            get 
            {
                return NumberPropertyValue(CrmStr.closeprobability);
            }
            set 
            {
                AddNumberProperty(CrmStr.closeprobability,value);
            }
        }

        /// <summary>
        /// Gets or sets how the estimated revenue is calculated, by the system or by the user. 
        /// </summary>
        [CrmDynAttribute(CrmStr.opportunity)]
        public bool isrevenuesystemcalculated 
        {
            get 
            {
                return BooleanPropertyValue(CrmStr.isrevenuesystemcalculated);
            }
            set 
            {
                AddBooleanProperty(CrmStr.isrevenuesystemcalculated,value);
            }
        }
        
        [CrmDynAttribute(CrmStr.opportunity)]
        public int prioritycode 
        {
            get 
            {
                return PicklistPropertyIntValue(CrmStr.prioritycode);
            }
            set 
            {
                AddPicklistProperty(CrmStr.prioritycode,value);
            }
        }

        public string prioritycodeLabel
        {
            get
            {
                if (ContainsKey(CrmStr.prioritycode))
                    return PicklistPropertyLabelValue(CrmStr.prioritycode);
                return string.Empty;
            }
        }
        
        [CrmDynAttribute(CrmStr.opportunity)]
        public int salesstagecode 
        {
            get 
            {
                return PicklistPropertyIntValue(CrmStr.salesstagecode);
            }
            set 
            {
                AddPicklistProperty(CrmStr.salesstagecode,value);
            }
        }

        public string salesstagecodeLabel
        {
            get
            {
                if (ContainsKey(CrmStr.salesstagecode))
                    return PicklistPropertyLabelValue(CrmStr.salesstagecode);
                return string.Empty;
            }
        }
        
        /// <summary>
        /// Gets or sets the ID of the step in the sales process. 
        /// </summary>
        [CrmDynAttribute(CrmStr.opportunity)]
        public Guid stepid
        {
            get 
            {
                return UniqueIdentifierPropertyValue(CrmStr.stepid);
            }
            set
            {
                AddUniqueIdentifierProperty(CrmStr.stepid,value);
            }
        }

        /// <summary>
        /// Gets or sets the name of the step in the sales process. 
        /// </summary>
        [CrmDynAttribute(CrmStr.opportunity)]
        public string stepname
        {
            get 
            {
                return StringPropertyValue(CrmStr.stepname);
            }
            set
            {
                AddStringProperty(CrmStr.stepname,value);
            }
        }

        /// <summary>
        /// virtual because on 'detail' records it is only a uniqueidentifier property
        /// </summary>
        [CrmDynAttribute(CrmStr.opportunity)]
        public Guid owningbusinessunit
        {
            get
            {
                return LookupPropertyValue(CrmStr.owningbusinessunit);
            }
            set
            {
                AddLookupProperty(CrmStr.owningbusinessunit,CrmStr.businessunit,value);
            }
        }
        
        public string owningbusinessunitName
        {
            get
            {
                return LookupPropertyName(CrmStr.owningbusinessunit);
            }
        }

        /// <summary>
        /// Owner for this object
        /// </summary>
        [CrmDynAttribute(CrmStr.opportunity)]
        public override Guid ownerid
        {
            get
            {
                if ( ContainsKey(CrmStr.ownerid) )
                    return OwnerPropertyValue(CrmStr.ownerid);
                return Guid.Empty;
            }
            set
            {
                AddOwnerProperty(CrmStr.ownerid,CrmStr.systemuser,value);
            }
        }
    }
}
